from keras.datasets import mnist
from keras import models
from keras import layers
from keras.utils.np_utils import to_categorical
import matplotlib.pyplot as plt

# 定义模型
def model_conv():
    model = models.Sequential()
    # 卷积层 32 个滤波器
    model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)))
    # 最大池化
    model.add(layers.MaxPooling2D((2, 2)))
    # 卷积层 64 个滤波器
    model.add(layers.Conv2D(64, (3, 3), activation='relu'))
    # 最大池化
    model.add(layers.MaxPooling2D((2, 2)))
    # 卷积层 64 个滤波器
    model.add(layers.Conv2D(64, (3, 3), activation='relu'))
    # 二维转一维 进入全连接层
    model.add(layers.Flatten())
    model.add(layers.Dense(64, activation='relu'))
    # 输出层 使用 softmax作为激活函数
    model.add(layers.Dense(10, activation='softmax'))
    # 优化器（RMSprop）损失函数（交叉熵）训练过程中监测的指标（准确度）
    model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['acc'])
    return model

if __name__ == "__main__":
    # 导入MNIST数据集
    (train_data, train_labels), (test_data, test_labels) = mnist.load_data()
    print('train_shape {} {}'.format(train_data.shape,train_labels.shape))
    print('test_shape {} {}'.format(test_data.shape,test_labels.shape))
    plt.imshow(train_data[0])
    plt.title('number {}'.format(train_labels[0]))
    plt.show()

    # 数据预处理
    x_train = train_data.reshape((60000, 28, 28, 1))
    x_train = x_train.astype('float32')/255
    x_test = test_data.reshape((10000, 28, 28, 1))
    x_test = x_test.astype('float32')/255
    y_train = to_categorical(train_labels)
    y_test = to_categorical(test_labels)
    print(x_train.shape, y_train.shape)

    # 定义模型
    model = model_conv()
    print(model.summary())

    # 开始训练
    his = model.fit(x_train, y_train, epochs=5, batch_size=64, validation_split=0.1)

    # 计算准确度
    loss, acc = model.evaluate(x_test, y_test)
    print('loss {}, acc {}'.format(loss, acc))

    # 保存模型
    model.save("model.h5")


	# (60000, 28, 28, 1) (60000, 10)
	# loss 0.02437469101352144, acc 0.9927
	# 99.27%